﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KingSite.SyncDB.Core.Entity;
using KingSite.SyncDB.Core.Repository;

namespace KingSite.SyncDBManager {
    public partial class MainFrm : Form {

        SyncRepository sr = new SyncRepository();
        SyncJobInfo job = new SyncJobInfo();
        public MainFrm() {
            InitializeComponent();
        }

        public void Init() {
            IList<SyncJobInfo> jobList = sr.GetAllSyncJob();
            dataGridView1.DataSource = jobList;
        }

        private void MainFrm_Load(object sender, EventArgs e) {
            Init();
        }

        private void DBToolStripMenuItem_Click(object sender, EventArgs e) {
            DBSetupFrm frm = new DBSetupFrm();
            frm.ShowDialog();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
            if (dataGridView1.SelectedCells.Count != 0) {
                //得到选中行某列的值
                job.SdbId =  Convert.ToInt32(dataGridView1.CurrentRow.Cells["sdbid"].Value);
                job.StId = Convert.ToInt32(dataGridView1.CurrentRow.Cells["stid"].Value);
                job.DdbId = Convert.ToInt32(dataGridView1.CurrentRow.Cells["ddbid"].Value);
                job.DtId = Convert.ToInt32(dataGridView1.CurrentRow.Cells["dtid"].Value);
            }

        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e) {
            ViewCurrentJob();
        }

        private void ViewCurrentJob() {
            SyncJobDetailView view = new SyncJobDetailView();
            view.job = job;
            view.ShowDialog();
        }

        private void SyncJobToolStripMenuItem_Click(object sender, EventArgs e) {
            SyncJobDetailAdd add = new SyncJobDetailAdd();
            add.ShowDialog();
        }

        private void Btn_Refresh_Click(object sender, EventArgs e) {
            Init();
        }

        private void ViewCurrentJobToolStripMenuItem_Click(object sender, EventArgs e) {
            ViewCurrentJob();
        }

        private void DelCurrentJobToolStripMenuItem_Click(object sender, EventArgs e) {
            DelCurrentJob();
        }

        private void DelCurrentJob() { 
            sr.ClearSync(job.SdbId, job.StId);
            Init();
        }
    }
}
